package drops.gui;

import java.awt.Color;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.JMenu;
import javax.swing.JMenuItem;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import javax.swing.ImageIcon;
import javax.swing.JPopupMenu;

import drops.dao.Tooth;
import drops.dao.ToothPeer;



public class DentalDiagramPanel extends JPanel implements MouseListener, ActionListener {
	DentalRecordPanel dentalRecordPanel;
	
	JPanel[] gridPanel;
	
	JLabel[] teethNumberLabels;
	JLabel[] blankTeethLabels;
	JLabel[] teethLabels;
	JLabel[] valueLabels;
	
	ImageIcon[] teethIcons;
	ImageIcon[] defaultIcons;
	JPopupMenu mainMenu;
	JMenuItem[] menuItem;
	int patientNumber;
	int toothNumber;
	
	public DentalDiagramPanel(DentalRecordPanel dentalRecordPanel) {
		this.dentalRecordPanel = dentalRecordPanel;

		setLayout(new GridLayout(4,1));
		
		teethIcons = new ImageIcon[25];
		
		for (int i=0;i<25;i++) {
			teethIcons[i] = new ImageIcon("images/teethmarkings/teeth_icon"+i+".png");
		}
		
		defaultIcons = new ImageIcon[3];
		
		for (int i=0;i<3;i++) {
			defaultIcons[i] = new ImageIcon("images/teethmarkings/default"+(i+1)+".png");
		}
		
		teethNumberLabels = new JLabel[52];
		blankTeethLabels = new JLabel[36];
		teethLabels = new JLabel[52];
		valueLabels = new JLabel[52];
		
		gridPanel = new JPanel[4];
		
		for(int i=0;i<4;i++){
			if(i==0||i==3)
				gridPanel[i] = new JPanel(new GridLayout(3,16));
			else {
				gridPanel[i] = new JPanel(new GridLayout(3,16));
			}
		}
		
		
		for(int i=0;i<36;i++){
			blankTeethLabels[i] = new JLabel();
		}
		
		for(int i=0;i<5;i++){teethNumberLabels[i] = new JLabel(" "+(55-i));}
		for(int i=5;i<10;i++){teethNumberLabels[i] = new JLabel(" "+(i+56));}
		
		for(int i=10;i<18;i++){teethNumberLabels[i] = new JLabel(" "+(28-i));}
		for(int i=18;i<26;i++){teethNumberLabels[i] = new JLabel(" "+(i+3));}
		for(int i=26;i<34;i++){teethNumberLabels[i] = new JLabel(" "+(74-i));}
		for(int i=34;i<42;i++){teethNumberLabels[i] = new JLabel(" "+(i-3));}
		
		for(int i=42;i<47;i++){teethNumberLabels[i] = new JLabel(" "+(127-i));}
		for(int i=47;i<52;i++){teethNumberLabels[i] = new JLabel(" "+(i+24));}
		
		for(int i=0;i<52;i++) {
			teethLabels[i] = new JLabel(defaultIcons[0]);
			teethLabels[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
			valueLabels[i] = new JLabel(teethIcons[0]);
			valueLabels[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
			teethNumberLabels[i].setBorder(BorderFactory.createLineBorder(Color.BLACK));
			teethNumberLabels[i].setBackground(Color.WHITE);
			teethNumberLabels[i].setOpaque(true);
			valueLabels[i].addMouseListener(this);
		}
		
		for(int i=0;i<3;i++){gridPanel[0].add(blankTeethLabels[i]);}
		for(int i=0;i<10;i++){gridPanel[0].add(valueLabels[i]);}
		for(int i=0;i<6;i++){gridPanel[0].add(blankTeethLabels[i+3]);}
		for(int i=0;i<10;i++){gridPanel[0].add(teethNumberLabels[i]);}
		for(int i=0;i<6;i++){gridPanel[0].add(blankTeethLabels[i+9]);}
		for(int i=0;i<10;i++){gridPanel[0].add(teethLabels[i]);}
		for(int i=0;i<3;i++){gridPanel[0].add(blankTeethLabels[i+15]);}
	
		for(int i=0;i<16;i++){gridPanel[1].add(valueLabels[i+10]);}
		for(int i=0;i<16;i++){gridPanel[1].add(teethNumberLabels[i+10]);}
		for(int i=0;i<16;i++){gridPanel[1].add(teethLabels[i+10]);}
		for(int i=0;i<16;i++){gridPanel[2].add(teethLabels[i+26]);}
		for(int i=0;i<16;i++){gridPanel[2].add(teethNumberLabels[i+26]);}
		for(int i=0;i<16;i++){gridPanel[2].add(valueLabels[i+26]);}
		
		for(int i=0;i<3;i++){gridPanel[3].add(blankTeethLabels[i+18]);}
		for(int i=0;i<10;i++){gridPanel[3].add(teethLabels[i+42]);}
		for(int i=0;i<6;i++){gridPanel[3].add(blankTeethLabels[i+21]);}
		for(int i=0;i<10;i++){gridPanel[3].add(teethNumberLabels[i+42]);}
		for(int i=0;i<6;i++){gridPanel[3].add(blankTeethLabels[i+27]);}
		for(int i=0;i<10;i++){gridPanel[3].add(valueLabels[i+42]);}
		for(int i=0;i<3;i++){gridPanel[3].add(blankTeethLabels[i+33]);}
		

		mainMenu = new JPopupMenu();
		menuItem = new JMenuItem[25];
		JMenu[] subMenu = new JMenu[3];
		
		subMenu[0] = new JMenu("Symbols for Mouth Examination");
		subMenu[1] = new JMenu("Symbols for Accomplishment");
		subMenu[2] = new JMenu("Artificial Restoration");
		
		menuItem[0] = new JMenuItem("No Symbol");
		menuItem[0].addActionListener(this);
		
		menuItem[1] = new JMenuItem("for extraction");
		menuItem[1].addActionListener(this);
		menuItem[2] = new JMenuItem("for filling");
		menuItem[2].addActionListener(this);
		menuItem[3] = new JMenuItem("with temporary filling");
		menuItem[3].addActionListener(this);
		menuItem[4] = new JMenuItem("recurrent caries");
		menuItem[4].addActionListener(this);
		menuItem[5] = new JMenuItem("roof fragment");
		menuItem[5].addActionListener(this);
		menuItem[6] = new JMenuItem("missing tooth");
		menuItem[6].addActionListener(this);		
		menuItem[7] = new JMenuItem("decubital ulcer");
		menuItem[7].addActionListener(this);
		menuItem[8] = new JMenuItem("maloclussion");
		menuItem[8].addActionListener(this);
		menuItem[9] = new JMenuItem("fluorosis");
		menuItem[9].addActionListener(this);
		menuItem[10] = new JMenuItem("normal");
		menuItem[10].addActionListener(this);
		menuItem[11] = new JMenuItem("moderate gingivitis");
		menuItem[11].addActionListener(this);
		menuItem[12] = new JMenuItem("severe gingivitis");
		menuItem[12].addActionListener(this);
		menuItem[13] = new JMenuItem("complete mouth rehab");
		menuItem[13].addActionListener(this);
		menuItem[14] = new JMenuItem("sound erupted");
		menuItem[14].addActionListener(this);
		for(int i=0;i<15;i++){subMenu[0].add(menuItem[i]);}

		menuItem[15] = new JMenuItem("extracted permanent tooth");
		menuItem[15].addActionListener(this);
		menuItem[16] = new JMenuItem("extracted temporary tooth");
		menuItem[16].addActionListener(this);
		menuItem[17] = new JMenuItem("amalgam filling");
		menuItem[17].addActionListener(this);
		menuItem[18] = new JMenuItem("composite filling");
		menuItem[18].addActionListener(this);
		for(int i=15;i<19;i++){subMenu[1].add(menuItem[i]);}
		
		menuItem[19] = new JMenuItem("jacket crown");
		menuItem[19].addActionListener(this);
		menuItem[20] = new JMenuItem("inlay");
		menuItem[20].addActionListener(this);
		menuItem[21] = new JMenuItem("oral prophylaxis");
		menuItem[21].addActionListener(this);
		menuItem[22] = new JMenuItem("zinc oxide uegenol filling");
		menuItem[22].addActionListener(this);
		menuItem[23] = new JMenuItem("temporary filling");
		menuItem[23].addActionListener(this);
		menuItem[24] = new JMenuItem("referred");
		menuItem[24].addActionListener(this);
		for(int i=19;i<25;i++){subMenu[2].add(menuItem[i]);}
		
		mainMenu.add(menuItem[0]);
		mainMenu.add(subMenu[0]);
		mainMenu.add(subMenu[1]);
		mainMenu.add(subMenu[2]);
		
		add(gridPanel[0]);
		add(gridPanel[1]);
		add(gridPanel[2]);
		add(gridPanel[3]);
	}
	
	public void setPatient(int patientNumber) {
		this.patientNumber = patientNumber;
		updateTeeth();
	}
	
	public void updateTeeth() {
		List <Tooth> tList= ToothPeer.searchTooth(patientNumber);
		if ((tList) == null) return;
		for (int i=0;i<52;i++) {
			Tooth t = tList.get(i);
			valueLabels[i].setIcon(teethIcons[t.getValue()]);
		}
	}
	
	@Override
	public void mouseClicked(MouseEvent arg0) {
		// TODO Auto-generated method stub
		Object o = arg0.getSource();
		//if (arg0.isPopupTrigger()) {
			for (int i=0;i<52;i++) {
				if (o==valueLabels[i]) {
					int x =arg0.getX();
					int y =arg0.getY();
					toothNumber=i;
					mainMenu.show(valueLabels[i], x, y);
					return;
				}
			}
		//}
	}
	@Override
	public void mouseEntered(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseExited(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mousePressed(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}
	@Override
	public void mouseReleased(MouseEvent arg0) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		// TODO Auto-generated method stub
		Object o = arg0.getSource();
		for (int i=0;i<25;i++) {
			if (o==menuItem[i]) {
				if (valueLabels[toothNumber].getIcon()==teethIcons[i]) return;
				valueLabels[toothNumber].setIcon(teethIcons[i]);
				
				ToothPeer.editTooth(toothNumber, patientNumber, i, "default");
				return;
			}
		}
	}
}
